﻿#include "VAOneXMLAdaptorPhysicalProperty.h"
#include "VAOneXMLAdaptorPhysicalPropertyBeam.h"
#include "VAOneXMLAdaptorPhysicalPropertyUniform.h"
#include "VAOneXMLAdaptorPhysicalPropertyRibbed.h"
#include "VAOneXMLAdaptorPhysicalPropertyGeneralLaminate.h"
#include "Tools/Win64/rapidxml/rapidxml.hpp"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyGeneralLaminate.h"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyGeneralLaminateIsoLayer.h"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyRibInfo.h"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyRibbed.h"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyUniform.h"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyBeam.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"

namespace IO
{
    bool IO::VAOneXMLAdaptorPhysicalProperty::adaptR()
    {  
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        _physicsPropDataManager = dynamic_cast<Acoustics::FITKAcousticsPhysicsPropManager*>(_dataObj);

        if(!_physicsPropDataManager)
        {
            return false;
        }

        bool b = true;
        b &= readBeam();
        b &= readShellPhysicalProperties();
        // 返回操作是否成功
        return b;
    }

    bool VAOneXMLAdaptorPhysicalProperty::adaptW()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        _physicsPropDataManager = dynamic_cast<Acoustics::FITKAcousticsPhysicsPropManager*>(_dataObj);

        if(!_physicsPropDataManager)
        {
            return false;
        }

        bool b = true;
        b &= writeBeam();
        b &= writeShellPhysicalProperties();
        // 返回操作是否成功
        return b;
    }

    bool VAOneXMLAdaptorPhysicalProperty::readBeam()
    {
        rapidxml::xml_node<char>* node = _node->first_node("BeamPhysicalProperty");

        if (!node)// 判断Fluid节点是否存在
        {
            return true;
        }

        bool b = true;
        Acoustics::FITKPhyscisPropertyBeam* pData{};
        IO:: VAOneXMLAdaptorPhysicalPropertyBeam* adaptor{};

        // 循环处理每个Beam节点数据
        while (node)
        {
            pData = new Acoustics::FITKPhyscisPropertyBeam;

            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorPhysicalPropertyBeam>("VAOneXML", pData);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(pData);
            b &= adaptor->adaptR();

            //根据名称获取对象
            auto m = _physicsPropDataManager->getDataByName(pData->getDataObjectName());

            if(!m)
            {
                //返回为空 插入mData
                _physicsPropDataManager->appendDataObj(pData);
            }
            else
            {
                //返回不为空， 覆盖数据
                m->copy(pData);
                delete pData;
            }

            node = node->next_sibling("BeamPhysicalProperty");
        }

        delete adaptor;
        adaptor = nullptr;
        //读取成功
        return b;
    }

    bool VAOneXMLAdaptorPhysicalProperty::writeBeam()
    {
        //根据类型获取多层数据集合
        QList<Acoustics::FITKAcousticsAbsPhysicsProp*> dataList = _physicsPropDataManager->getPhysicsProps(Acoustics::FITKAcousticsAbsPhysicsProp::AcousticsPhyscisPropType::APPBeam);
        bool b = true;
        //获取数据集合数量
        int dataCount = dataList.count();
        IO:: VAOneXMLAdaptorPhysicalPropertyBeam* adaptor{};

        for(int i = 0; i < dataCount; i++)
        {
            auto beamData = dataList[i];

            if(!beamData)
            {
                continue;
            }

            //创建多层节点
            rapidxml::xml_node<char>* node = createNodeElement(_node, "BeamPhysicalProperty");
            if(!adaptor)
            {
                //创建多层节点操作器
               adaptor  = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorPhysicalPropertyBeam>("VAOneXML", beamData);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(beamData);
            b &= adaptor->adaptW();
        }

        delete adaptor;
        adaptor = nullptr;
        return b;
    }

    bool VAOneXMLAdaptorPhysicalProperty::readUniform(rapidxml::xml_node<char> *node)
    {
        auto Uniform = node->first_node("UniformPhysicalProperty");

        if (!Uniform)// 判断Fluid节点是否存在
        {
            return true;
        }

        bool b = true;
        Acoustics::FITKPhyscisPropertyUniform* pData{};
        IO:: VAOneXMLAdaptorPhysicalPropertyUniform* adaptor{};

        // 循环处理每个Beam节点数据
        while (Uniform)
        {
            pData = new Acoustics::FITKPhyscisPropertyUniform;

            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorPhysicalPropertyUniform>("VAOneXML", pData);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(Uniform);
            adaptor->setDataObject(pData);
            b &= adaptor->adaptR();

            //根据名称获取对象
            auto m = _physicsPropDataManager->getDataByName(pData->getDataObjectName());

            if(!m)
            {
                //返回为空 插入mData
                _physicsPropDataManager->appendDataObj(pData);
            }
            else
            {
                //返回不为空， 覆盖数据
                m->copy(pData);
                delete pData;
            }

            Uniform = Uniform->next_sibling("UniformPhysicalProperty");
        }

        delete adaptor;
        adaptor = nullptr;

        //读取成功
        return b;

    }

    bool VAOneXMLAdaptorPhysicalProperty::writeUniform()
    {
        //根据类型获取多层数据集合
        QList<Acoustics::FITKAcousticsAbsPhysicsProp*> dataList = _physicsPropDataManager->getPhysicsProps(Acoustics::FITKAcousticsAbsPhysicsProp::AcousticsPhyscisPropType::APPUniformPlat);
        bool b = true;
        //获取数据集合数量
        int dataCount = dataList.count();
        IO:: VAOneXMLAdaptorPhysicalPropertyUniform* adaptor{};

        for(int i = 0; i < dataCount; i++)
        {
            auto NCTLayUpData = dataList[i];

            if(!NCTLayUpData)
            {
                continue;
            }

            //创建多层节点
            rapidxml::xml_node<char>* node = createNodeElement(_shellPhysicalPropertiesNode, "UniformPhysicalProperty");
            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorPhysicalPropertyUniform>("VAOneXML", NCTLayUpData);
            }
            //创建多层节点操作器
            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(NCTLayUpData);
            b &= adaptor->adaptW();
        }

        delete adaptor;
        adaptor = nullptr;
        return b;
    }

    bool VAOneXMLAdaptorPhysicalProperty::readSandwichPhysicalProperty()
    {
        return true;
    }

    bool VAOneXMLAdaptorPhysicalProperty::readGeneralLaminatePhysicalProperty(rapidxml::xml_node<char> *node)
    {
        auto glNode = node->first_node("GeneralLaminatePhysicalProperty");

        if (!glNode)// 判断Fluid节点是否存在
        {
            return true;
        }

        bool b = true;
        IO:: VAOneXMLAdaptorPhysicalPropertyGeneralLaminate* adaptor{};

        // 循环处理每个Beam节点数据
        while (glNode)
        {
            Acoustics::FITKPhyscisPropertyGeneralLaminate* pData = new Acoustics::FITKPhyscisPropertyGeneralLaminate;

            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorPhysicalPropertyGeneralLaminate>("VAOneXML", pData);
            }
            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(glNode);
            adaptor->setDataObject(pData);
            b &= adaptor->adaptR();

            //根据名称获取对象
            auto m = _physicsPropDataManager->getDataByName(pData->getDataObjectName());

            if(!m)
            {
                //返回为空 插入mData
                _physicsPropDataManager->appendDataObj(pData);
            }
            else
            {
                //返回不为空， 覆盖数据
                m->copy(pData);
                delete pData;
            }

            glNode = glNode->next_sibling("GeneralLaminatePhysicalProperty");
        }

        delete adaptor;
        adaptor = nullptr;
        //读取成功
        return b;
    }

    bool VAOneXMLAdaptorPhysicalProperty::writeGeneralLaminatePhysicalProperty()
    {
        //根据类型获取多层数据集合
        QList<Acoustics::FITKAcousticsAbsPhysicsProp*> dataList = _physicsPropDataManager->getPhysicsProps(Acoustics::FITKAcousticsAbsPhysicsProp::AcousticsPhyscisPropType::APPGeneralLaminatePhysicalProperty);
        bool b = true;
        //获取数据集合数量
        int dataCount = dataList.count();
        //创建多层节点操作器
        IO:: VAOneXMLAdaptorPhysicalPropertyGeneralLaminate* adaptor{};

        for(int i = 0; i < dataCount; i++)
        {
            auto NCTLayUpData = dataList[i];

            if(!NCTLayUpData)
            {
                continue;
            }

            //创建多层节点
            rapidxml::xml_node<char>* node = createNodeElement(_shellPhysicalPropertiesNode, "GeneralLaminatePhysicalProperty");
            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorPhysicalPropertyGeneralLaminate>("VAOneXML", NCTLayUpData);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(NCTLayUpData);
            b &= adaptor->adaptW();
        }

        delete adaptor;
        adaptor = nullptr;
        return b;
    }

    bool VAOneXMLAdaptorPhysicalProperty::readRibbedPhysicalProperty(rapidxml::xml_node<char> *node)
    {
        auto ribbedNode = node->first_node("RibbedPhysicalProperty");

        if (!ribbedNode)// 判断Fluid节点是否存在
        {
            return true;
        }

        bool b = true;
        IO:: VAOneXMLAdaptorPhysicalPropertyRibbed* adaptor{};

        // 循环处理每个Beam节点数据
        while (ribbedNode)
        {
            Acoustics::FITKPhyscisPropertyRibbed* pData = new Acoustics::FITKPhyscisPropertyRibbed;
            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorPhysicalPropertyRibbed>("VAOneXML", pData);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(ribbedNode);
            adaptor->setDataObject(pData);
            b &= adaptor->adaptR();

            //根据名称获取对象
            auto m = _physicsPropDataManager->getDataByName(pData->getDataObjectName());

            if(!m)
            {
                //返回为空 插入mData
                _physicsPropDataManager->appendDataObj(pData);
            }
            else
            {
                //返回不为空， 覆盖数据
                m->copy(pData);
                delete pData;
            }

            ribbedNode = ribbedNode->next_sibling("RibbedPhysicalProperty");
        }

        delete adaptor;
        adaptor = nullptr;
        //读取成功
        return b;
    }

    bool VAOneXMLAdaptorPhysicalProperty::writeRibbedPhysicalProperty()
    {
        //根据类型获取多层数据集合
        QList<Acoustics::FITKAcousticsAbsPhysicsProp*> dataList = _physicsPropDataManager->getPhysicsProps(Acoustics::FITKAcousticsAbsPhysicsProp::AcousticsPhyscisPropType::APPRibbedphysicalProperty);
        bool b = true;
        //获取数据集合数量
        int dataCount = dataList.count();
        //创建多层节点操作器
        IO:: VAOneXMLAdaptorPhysicalPropertyRibbed* adaptor{};

        for(int i = 0; i < dataCount; i++)
        {
            auto ribData = dataList[i];

            if(!ribData)
            {
                continue;
            }

            //创建节点
            rapidxml::xml_node<char>* node = createNodeElement(_shellPhysicalPropertiesNode, "RibbedPhysicalProperty");
            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorPhysicalPropertyRibbed>("VAOneXML", ribData);
            }
            //创建多层节点操作器

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(ribData);
            b &= adaptor->adaptW();
            delete adaptor;
            adaptor = nullptr;
        }
        return b;
    }

    bool VAOneXMLAdaptorPhysicalProperty::readCompositePhysicalProperty()
    {
        return true;
    }

    bool VAOneXMLAdaptorPhysicalProperty::readShellPhysicalProperties()
    {
        rapidxml::xml_node<char>* shellNode = _node->first_node("ShellPhysicalProperties");

        if(!shellNode)
        {
            return false;
        }

        bool b;

        // 1. 读取均质板 ： UniformPhysicalProperty
        b = readUniform(shellNode);

        if(!b)
        {
            //读写器创建异常
            consoleMessage(3,"Read Uniform Error");
        }

        // 5.加筋板 ： RibbedPhysicalProperty
        b = readRibbedPhysicalProperty(shellNode);

        if(!b)
        {
            //读写器创建异常
            consoleMessage(3,"Read RibbedPhysicalProperty Error");
        }

        // 4.自由阻尼层合板 ： GeneralLaminatePhysicalProperty
        b = readGeneralLaminatePhysicalProperty(shellNode);

        if(!b)
        {
            //读写器创建异常
            consoleMessage(3,"Read GeneralLaminatePhysicalProperty Error");
        }

        return true;

    }

    bool VAOneXMLAdaptorPhysicalProperty::writeShellPhysicalProperties()
    {
        //创建多层节点
        _shellPhysicalPropertiesNode = createNodeElement(_node, "ShellPhysicalProperties");

        if(!_shellPhysicalPropertiesNode)
        {
            return false;
        }

        bool b;

        // 1. 读取均质板 ： UniformPhysicalProperty
        b = writeUniform();

        if(!b)
        {
            //读写器创建异常
            consoleMessage(3,"write Uniform Error");
        }

        // 5.加筋板 ： RibbedPhysicalProperty
        b = writeRibbedPhysicalProperty();

        if(!b)
        {
            //读写器创建异常
            consoleMessage(3,"write RibbedPhysicalProperty Error");
        }

        // 4.自由阻尼层合板 ： GeneralLaminatePhysicalProperty
        b = writeGeneralLaminatePhysicalProperty();

        if(!b)
        {
            //读写器创建异常
            consoleMessage(3,"write GeneralLaminatePhysicalProperty Error");
        }

        return true;
    }
}
